package cn.edu.ljl;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

//对学习建议信息进行修改
//修改成功后、进入学习建议信息展示界面，使用listExperience

public class EditExperience extends HttpServlet {

    public static String Id;
//    public static String Name;
//    public static String cou;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Id=id;

        String sql = "SELECT * FROM courses WHERE id=" + id;
        try {
            List<Experience> experiences = DBMS.getExperience(sql);
            if (experiences.size() == 0) {
                response.sendRedirect("./listExperience");
                return;
            }

            Experience experience = experiences.get(0);

            StringBuilder sb = new StringBuilder();
            sb.append(Page.getHeader());
            sb.append(buildStudentForm(experience));
            sb.append(Page.getEnd());

            response.setCharacterEncoding("UTF-8");
            try (Writer writer = response.getWriter()) {
                writer.write(sb.toString());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public String buildStudentForm(Experience experience) {
        String html = String.format("<center>\n" +
                        "<br>\n" +
                        "<form action='./updateExperience' method='POST''>\n" +
                        "<table style='background-color:#dddddd;padding:2em'>\n" +
                        "\t<tr><td>您的名字:</td><td><font face=\"宋体\" >%s</font></td></tr>\n" +
                        "\t<tr><td>学科名字:</td><td><font face=\"宋体\" >%s</font></td></tr>\n" +
                        "\t<tr><td>您的建议:</td><td><textarea name=advice cols='50' rows='8'>%s</textarea></td></tr>\n" +
                        "\t<tr><td colspan='2' align='center'><br><input type='submit' value='提交修改'></td></tr>\n" +
                        "</table>\t\n" +
                        "</form>\n" +
                        "</div>\n" +
                        "</center>",
                experience.getName(), experience.getCourse(), experience.getAdvice()
        );
        return html;
    }
}
